/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package com.dianning.dao.hibernate;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dianning.bean.AdAds;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.dao.AdAdsDAO;
import com.dianning.dao.VisitorDAO;

/**
 * 功能描述:广告操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class AdAdsHibernateDAO extends HibernateDaoSupport implements AdAdsDAO {

	private static final String LOAD_BY_ID = "from AdAds where id = ?";
	
	/**
	 * 保存AdAds对象
	 * 
	 * @param a
	 * 			AdAds对象
	 * @return AdAds
	 * 			AdAds对象
	 */
	public AdAds saveAdAds(AdAds ad){
		this.getHibernateTemplate().save(ad);
		return ad;
	}
	/**
	 * 保存或更新AdAds对象
	 * 
	 * @param a
	 * 			AdAds对象
	 * @return AdAds
	 * 			AdAds对象
	 */
	public AdAds saveOrUpdateAdAds(AdAds ad){
		this.getHibernateTemplate().saveOrUpdate(ad);
		return ad;
	}
	/**
	 * 保存AdAds对象
	 * 
	 * @param a
	 * 			AdAds对象
	 * @return AdAds
	 * 			AdAds对象
	 */
	public AdAds updateAdAds(AdAds ad){
		this.getHibernateTemplate().update(ad);
		return ad;
	}
	
	/**
	 * 根据主键ID获取AdAds对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return AdAds
	 * 			AdAds对象
	 */
	public AdAds findAdAdsById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (AdAds) l.get(0);
		}
	}
	
	/**
	 * 获取AdAds个数
	 * 
	 * @param sid
	 * 			广告位ID
	 * @param endTime
	 * 			是否过期
	 * @return int
	 * 			AdAds对象个数
	 */
	public long getAdAdsNum(final long sid,final int endTime){
		List l = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(AdAds.class);
				c.setProjection(Projections.count("id"));
				if (sid != -1) {
					c.add(Restrictions.eq("sid", new Long(sid)));
				}
				if (endTime != -1) {
					c.add(Restrictions.ge("endTime", new Date()));
				}
				return c.list();
			}
		});
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return ((Long) l.get(0)).longValue();
		}
	}
	
	/**
	 * 获取AdAds列表
	 * 
	 * @param sid
	 * 			广告位ID
	 * @param endTime
	 * 			是否过期
	 * @param orderby
	 *          String
	 * @param ascOrDesc
	 *          int
	 * @return List
	 * 			AdAds对象列表
	 */
	public List findAdAds(final long sid,final int endTime, final String orderby, final int ascOrDesc){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(AdAds.class);
				if (sid != -1) {
					c.add(Restrictions.eq("sid", new Long(sid)));
				}
				if (endTime != -1) {
					c.add(Restrictions.ge("endTime", new Date()));
				}
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				return c.list();
			}
		});
	}
}
